package com.dragon.modules.yuegou.service.impl.system;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.entity.system.PlatformUserEntity;
import com.dragon.modules.yuegou.enums.code.SystemCode;
import com.dragon.modules.yuegou.enums.system.PlatformUserStatus;
import com.dragon.modules.yuegou.mapper.system.PlatformUserMapper;
import com.dragon.modules.yuegou.model.system.PlatformUser;
import com.dragon.modules.yuegou.model.system.User;
import com.dragon.modules.yuegou.query.system.PlatformUserQuery;
import com.dragon.modules.yuegou.service.system.PlatformUserService;
import com.dragon.modules.yuegou.service.system.UserService;
import com.dragon.modules.yuegou.web.Check;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nullable;

/**
 * 大运营用户 服务实现
 *
 * @author pbuilder generated
 * @date 2019-03-19 08:59:41
 * @version 2.0
 */
@Slf4j
@Service("sysPlatformUserService")
public class PlatformUserServiceImpl
		extends ServiceImpl<PlatformUserMapper, PlatformUserEntity>
		implements PlatformUserService {

	@Autowired
	private UserService userService;

	@Override
	public PlatformUser login(String mobile, String password) {
		User user = userService.getPlatformUserByUsername(mobile);
		userService.login(user, password);
		PlatformUserEntity pu = getById(user.getId());
		if (pu == null) {
			log.info("用户存在，但是用户不是大运营平台的用户，mobile={}", mobile);
			throw new DragonException(SystemCode.ACCOUNT_PASSWORD_IS_INCORRECT);
		} else if (pu.getDel()) {
			log.info("用户存在，但是大运营平台用户被删除了，mobile={}", mobile);
			throw new DragonException(SystemCode.ACCOUNT_DELETED);
		} else if (PlatformUserStatus.DISABLED.equals(pu.getStatus())) {
			log.info("用户存在，但是大运营平台用户被禁用了，mobile={}", mobile);
			throw new DragonException(SystemCode.ACCOUNT_FROZEN);
		}
		// XXX 未激活状态？
		return new PlatformUser(pu);
	}

	@Override
	@Transactional
	public PlatformUser createAccount(String username, @Nullable String mobile, @Nullable String nickname,
			String password) {
		User sysUser = userService.getPlatformUserByUsername(username);
		if (sysUser != null) {
			throw new DragonException(SystemCode.ACCOUNT_EXISTS);
		}
		sysUser = userService.getPlatformUserByMobile(mobile);
		if (sysUser != null) {
			throw new DragonException(SystemCode.MOBILE_EXISTS);
		}
		sysUser = userService.createAccount(username, mobile, nickname, password);
		PlatformUser platformUser = new PlatformUser();
		platformUser.setId(sysUser.getId());
		platformUser.setStatus(PlatformUserStatus.ENABLED);
		platformUser.setDel(false);
		baseMapper.insert(platformUser);
		platformUser.setUser(sysUser);
		return platformUser;
	}

	@Override
	@Transactional
	public void deleteAccount(String userId) {
		preRemoveUser(userId);
		// 删除系统用户
		userService.deleteAccount(userId);
		// 删除集团用户
		removeById(userId);
	}

	@Override
	@Transactional
	public void enableUser(String userId) {
		userService.enableUser(userId);
		PlatformUser pu = new PlatformUser();
		pu.setId(userId);
		pu.setStatus(PlatformUserStatus.ENABLED);
		baseMapper.updateById(pu);
	}

	@Override
	@Transactional
	public void disableUser(String userId) {
		preRemoveUser(userId);
		userService.disableUser(userId);
		PlatformUser pu = new PlatformUser();
		pu.setId(userId);
		pu.setStatus(PlatformUserStatus.DISABLED);
		baseMapper.updateById(pu);
	}

	/**
	 * 删除或禁用用户的前置条件
	 * 
	 * @param userId
	 *            用户ID
	 */
	private void preRemoveUser(String userId) {
		Check.notEmpty(userId, "必须提供用户ID");
		PlatformUserQuery query = new PlatformUserQuery();
		query.setStatus(PlatformUserStatus.ENABLED);
		query.setDel(false);
		// 不能删除最后一个用户
		if (baseMapper.selectCount(query.toQueryWrapper()) <= 1) {
			throw new DragonException(SystemCode.CANNOT_DELETE_LAST_ACCOUNT);
		}

	}
}